package com.course.db;

import java.sql.ResultSet;

public class OpenCourse extends ExecuteDB {

	//members
	private int Id;
	private int teacher_id;
	private int course_id;
	private int check_status;
	private int num_choose_students;
	private String strSql;
	
	public OpenCourse()
	{
		this.Id = 0;
		this.teacher_id = 0;
		this.course_id = 0;
		this.check_status = 0;
		this.num_choose_students = 0;
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from open_course";
		return this.strSql;
	} 
	
	//add a OpenCourse
	public boolean add()
	{

		this.strSql="insert into open_course ";
		this.strSql=this.strSql + "(teacher_id,course_id,check_status,num_choose_students) ";
		this.strSql=this.strSql + "values('" + this.teacher_id + "','" + this.course_id+ "','" + this.check_status + "','" + this.num_choose_students+ "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from open_course limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//delete a OpenCourse by id
    public boolean delete(int OpenCourseID)
    {
       this.strSql = "delete from open_course where Id in (";
       this.strSql = this.strSql + OpenCourseID +")";

       boolean isDelete = super.exeSql(this.strSql);
       return isDelete;
    }
    
  //initialize OpenCourse information
 	public boolean init()
	{
		this.strSql="select * from `open_course` where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.teacher_id=rs.getInt("teacher_id");
				this.course_id=rs.getInt("course_id");
				this.check_status=rs.getInt("check_status");
				this.num_choose_students=rs.getInt("num_choose_students");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}
 	
 	//modify OpenCourse infomation
    public boolean modify_open_course()
	{
		this.strSql="update open_course set";
		this.strSql=this.strSql + " teacher_id=" + "'" + this.teacher_id + "',";
		this.strSql=this.strSql + " course_id=" + "'" + this.course_id + "',";
		this.strSql=this.strSql + " check_status=" + this.check_status + ",";
		this.strSql=this.strSql + " num_choose_students=" + this.num_choose_students;
		this.strSql=this.strSql + " where Id=" + this.Id;
		
		boolean isUpdate = super.exeSql(this.strSql);

		return isUpdate;
	}
    
  //show OpenCourse
    public ResultSet show_open_course()
	{
		this.strSql="select * from `open_course`";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public int getTeacher_id() {
		return teacher_id;
	}

	public void setTeacher_id(int teacher_id) {
		this.teacher_id = teacher_id;
	}

	public int getCourse_id() {
		return course_id;
	}

	public void setCourse_id(int course_id) {
		this.course_id = course_id;
	}

	public int getCheck_status() {
		return check_status;
	}

	public void setCheck_status(int check_status) {
		this.check_status = check_status;
	}

	public int getNum_choose_students() {
		return num_choose_students;
	}

	public void setNum_choose_students(int num_choose_students) {
		this.num_choose_students = num_choose_students;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}

    
}
